<template>
<el-dialog :append-to-body="true" :close-on-click-modal="false" class="add-dialog" :title="addForm.title" :visible.sync="addForm.show" width="500px">
    <el-form ref="ruleForm" :model="addForm.form" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" prop="projectName">
            <el-input clearable placeholder="请输入项目名称" v-model="addForm.form.projectName"></el-input>
        </el-form-item>
        <el-form-item label="项目代码" prop="projectCode">
            <el-input :maxlength="20" clearable placeholder="请输入项目代码" v-model="addForm.form.projectCode"></el-input>
        </el-form-item>
        <el-form-item label="项目描述">
            <el-input :maxlength="100" type="textarea" :rows="5" clearable placeholder="请输入项目描述" v-model="addForm.form.projectDesc"></el-input>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="addForm.show=false">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </span>
</el-dialog>
</template>

<script>
export default {
    name: 'AddDialog',
    props: {
        addForm: {
            type: Object,
            required: true
        }
    },
    data () {
        return {
            rules: {
                projectName: [
                    {
                        required: true,
                        message: '请输入项目名称',
                        trigger: 'blur'
                    },
                    {
                        min: 3,
                        max: 30,
                        message: '长度在 3 到 30 个字符',
                        trigger: 'blur'
                    }
                ],
                projectCode: [
                    {
                        required: true,
                        message: '请输入项目代码',
                        trigger: 'blur'
                    },
                    { pattern: /^[a-zA-Z][a-zA-Z0-9_]*$/, message: '必须包含字母数字以及下划线，并以字母开头' }
                ]
            }
        }
    },
    methods: {
        handleSave () {
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    this.$emit('save')
                } else {
                    return false
                }
            })
        },
        resetForm () {
            this.$refs['ruleForm'].resetFields()
        }
    }
}
</script>
